package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId,UserHolder.get().getId())
                    .in(CollectionUtil.isNotEmpty(ids),Address::getId,ids);
        addressMapper.delete(queryWrapper);
    }

    @Override
    public void update(Address address) {
        addressMapper.updateById(address);
    }

    @Override
    public Address findById(Long id) {
        Address address = addressMapper.selectById(id);
        return address;
    }

    @Override
    public void setUpAddress(Long id) {
        //设置默认地址就要先把其他的地址设成0，然后再将这个id的地址设成1
        LambdaUpdateWrapper<Address>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Address::getUserId,UserHolder.get().getId());
        Address address = new Address();
        address.setIsDefault(0);
        int update = addressMapper.update(address, updateWrapper);
        //然后再将这个id的地址设成1
        address.setIsDefault(1);
        LambdaUpdateWrapper<Address> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(Address::getUserId,UserHolder.get().getId())
                    .eq(Address::getId,id);
        addressMapper.update(address, updateWrapper1);
    }

    @Override
    public void add(Address address) {
        address.setUserId(UserHolder.get().getId());
        address.setIsDefault(0);
        addressMapper.insert(address);
    }

    @Override
    public List<Address> findAll() {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId,UserHolder.get().getId());
        List<Address> addresses = addressMapper.selectList(queryWrapper);
        return addresses;
    }

    @Override
    public Address findAddressDefault() {
        //根据用户id和isdefault查询
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId, UserHolder.get().getId())
                    .eq(Address::getIsDefault,1);
        Address address = addressMapper.selectOne(queryWrapper);
        return address;
    }

    //
}
